package hawk.example.jdk.lambda;

import java.util.Arrays;
import java.util.Date;
import java.util.function.BiFunction;

import javax.swing.JOptionPane;
import javax.swing.Timer;

public class MyFirstLambda {

    public static void main(String[] args) {
        var planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune" };
        System.out.println(Arrays.toString(planets));

        System.out.println("Sorted in dictionary order:");
        Arrays.sort(planets);
        System.out.println(Arrays.toString(planets));
        System.out.println("Sorted by length:");
        Arrays.sort(planets, (first, second) -> first.length() - second.length());
        System.out.println(Arrays.toString(planets));

        BiFunction<String, String, Integer> comp = (first, second) -> first.length() - second.length();

        repeat(10, i -> System.out.println("Countdown: " + (9 - i)));

        var timer = new Timer(1000, event -> System.out.println("The time is " + new Date()));
        timer.start();

        // keep program running until user selects "OK"
        JOptionPane.showMessageDialog(null, "Quit program?");
        System.exit(0);
    }

    public interface IntConsumer {
        void accept(int value);
    }

    public static void repeat(int n, IntConsumer action) {
        for (int i = 0; i < n; i++)
            action.accept(i);
    }
}
